package com.damon.android.interview.service;

/**
 * Created by Damon.Han on 2018/10/11 0011.
 *
 * @author Damon
 */

import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;

import com.damon.android.interview.R;

/**
 * A placeholder fragment containing a simple view.
 */
public class ServiceFragment extends Fragment implements View.OnClickListener {
    private Button button1;
    private Button button2;
    private Button button3;
    private Button button4;
    private Activity activity;
    /**
     * The fragment argument representing the section number for this
     * fragment.
     */
    private static final String ARG_SECTION_NUMBER = "section_number";

    public ServiceFragment() {
    }

    /**
     * Returns a new instance of this fragment for the given section
     * number.
     */
    public static ServiceFragment newInstance() {
        ServiceFragment fragment = new ServiceFragment();
        Bundle args = new Bundle();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_service, container, false);
        button1 = rootView.findViewById(R.id.button1);
        button2 = rootView.findViewById(R.id.button2);
        button3 = rootView.findViewById(R.id.button3);
        button4 = rootView.findViewById(R.id.button4);
        button1.setOnClickListener(this);
        button2.setOnClickListener(this);
        button3.setOnClickListener(this);
        button4.setOnClickListener(this);
        activity = getActivity();
        return rootView;
    }

    ServiceConnection connection = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            Service2.MyBinder binder = (Service2.MyBinder) service;
            binder.showLog();
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {

        }
    };

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.button1:
                Intent intent1 = new Intent(activity, Service1.class);
                activity.startService(intent1);
                break;
            case R.id.button2:
                Intent intent2 = new Intent(activity, Service1.class);
                activity.stopService(intent2);
                break;
            case R.id.button3:
                Intent intent3 = new Intent(activity, Service1.class);
                activity.startService(intent3);
                activity.bindService(intent3, connection, Context.BIND_AUTO_CREATE);
                break;
            case R.id.button4:
                activity.unbindService(connection);
                break;
        }
    }
}